<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <h1>🎓 后台管理系统</h1>
        <p>新生入学教育考试系统</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            style="width: 100%"
            :loading="loading"
            @click="handleLogin"
          >
            登 录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="tips">
        <p><strong>测试账号：</strong></p>
        <p>管理员 - 用户名: admin, 密码: 123456</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { authApi } from '@/api/auth'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456'
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true

      try {
        const response = await authApi.login(loginForm.username, loginForm.password)
        const { token, admin } = response.data

        // 保存登录信息
        localStorage.setItem(
          'adminUser',
          JSON.stringify({
            id: admin.id,
            username: admin.username,
            realName: admin.username, // 如果没有realName字段，使用username
            role: admin.role || 'admin',
            token: token,
            loginTime: new Date().toISOString()
          })
        )

        ElMessage.success('登录成功')

        setTimeout(() => {
          router.push('/admin/dashboard')
        }, 500)
      } catch (error) {
        loading.value = false
        // 错误信息已在request拦截器中处理
      }
    }
  })
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-container {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  padding: 50px 40px;
  width: 100%;
  max-width: 420px;
  border: 1px solid #e8e8e8;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h1 {
  color: #333;
  font-size: 28px;
  margin-bottom: 12px;
  font-weight: 600;
}

.login-header p {
  color: #888;
  font-size: 14px;
  font-weight: 400;
}

.tips {
  margin-top: 20px;
  padding: 16px;
  background: #f9f9f9;
  border-radius: 6px;
  font-size: 13px;
  color: #666;
  border: 1px solid #e8e8e8;
}

.tips p {
  margin: 6px 0;
  line-height: 1.6;
}

.tips strong {
  color: #333;
  font-weight: 500;
}
</style>

